package com.aw.baselib.base

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.text.method.ScrollingMovementMethod
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import com.aw.baselib.R
import com.aw.baselib.util.AwDataUtil.isEmpty
import com.aw.baselib.util.AwScreenUtils.Companion.isLandscape
import com.aw.baselib.util.AwSoftKeyboardUtil.hideSoftKeyboard

/**
 * Created by hzw
 */
class AwViewDialog(private val mContext: Context) {
    private var mTvTopHint: TextView? = null
    private var mTvContent: TextView? = null
    private var mTvCancel: TextView? = null
    private var mTvConfirm: TextView? = null
    private var tv_progress: TextView? = null
    private var btn_cancel: TextView? = null
    private var pb_loading: ProgressBar? = null
    private var iv_img: ImageView? = null
    private var et_content: EditText? = null
    private var tv_title: TextView? = null
    private var mSpiltLine: View? = null
    var dialog: Dialog? = null
        private set

    init {
        try {
            dialog = Dialog(mContext, R.style.prompt_dialog)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun showMsgDialog(msg: String?) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_prompt)
        initView(dialog!!)
        mTvContent!!.text = msg
        mTvContent!!.movementMethod = ScrollingMovementMethod.getInstance()
        mTvCancel!!.visibility = View.GONE
        mTvConfirm!!.setOnClickListener { dismiss() }
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.show()
    }

    /**
     * 版本更新对话框
     * @param msg
     * @param confirmListener
     */
    fun showUpdateDialog(
        msg: String?,
        showCancel: Boolean,
        confirmListener: View.OnClickListener?,
        cancelListener: View.OnClickListener?
    ) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_update)
        dialog!!.setCancelable(false)
        dialog!!.setCanceledOnTouchOutside(false)
        initView(dialog!!)
        mTvContent!!.text = msg
        mTvCancel!!.visibility = if (showCancel) View.VISIBLE else View.GONE
        mTvContent!!.movementMethod = ScrollingMovementMethod.getInstance()
        mTvConfirm!!.setOnClickListener { v -> //                dismiss();
            confirmListener?.onClick(v)
        }
        mTvCancel!!.setOnClickListener { v ->
            dismiss()
            cancelListener?.onClick(v)
        }
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.3, 0.3)
        } else {
            setDialogSize(mContext, 0.8, 0.3)
        }
        dialog!!.show()
    }

    /**
     * 版本下载对话框
     */
    fun showDownloadDialog() {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_download)
        initUpdateDownloadView(dialog!!)
        btn_cancel!!.visibility = View.GONE
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.show()
    }

    fun showDialog(msg: String?, confirmListener: View.OnClickListener, showCancelBtn: Boolean) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_prompt)
        initView(dialog!!)
        mTvContent!!.text = msg
        mTvContent!!.movementMethod = ScrollingMovementMethod.getInstance()
        mTvCancel!!.visibility = if (showCancelBtn) View.VISIBLE else View.GONE
        mTvConfirm!!.setOnClickListener { v ->
            confirmListener.onClick(v)
            dismiss()
        }
        mTvCancel!!.setOnClickListener { dismiss() }
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.show()
    }

    fun showDialog(
        msg: String?,
        showCancel: Boolean,
        confirmListener: View.OnClickListener?,
        cancelListener: View.OnClickListener?
    ) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_prompt)
        initView(dialog!!)
        mTvContent!!.text = msg
        mTvCancel!!.visibility = if (showCancel) View.VISIBLE else View.GONE
        mTvContent!!.movementMethod = ScrollingMovementMethod.getInstance()
        mTvCancel!!.setOnClickListener { v ->
            dismiss()
            cancelListener?.onClick(v)
        }
        mTvConfirm!!.setOnClickListener { v ->
            dismiss()
            confirmListener?.onClick(v)
        }
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.show()
    }

    fun showDialog(
        msg: String?,
        leftTxt: String?,
        rightTxt: String?,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_app_error)
        initView(dialog!!)
        mTvContent!!.text = msg
        mTvContent!!.movementMethod = ScrollingMovementMethod.getInstance()
        mTvCancel!!.text = leftTxt
        mTvConfirm!!.text = rightTxt
        mTvCancel!!.setOnClickListener { v ->
            dismiss()
            leftListener?.onClick(v)
        }
        mTvConfirm!!.setOnClickListener { v ->
            dismiss()
            rightListener?.onClick(v)
        }
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.setCancelable(true)
        dialog!!.setCanceledOnTouchOutside(true)
        dialog!!.show()
    }

    fun showDialog(
        msg: String?,
        imgId: Int,
        leftTxt: String?,
        rightTxt: String?,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_prompt_with_center_img)
        initImgDialogView(dialog!!)
        mTvContent!!.text = msg
        mTvContent!!.movementMethod = ScrollingMovementMethod.getInstance()
        mTvCancel!!.text = leftTxt
        mTvConfirm!!.text = rightTxt
        iv_img!!.setImageResource(imgId)
        mTvCancel!!.setOnClickListener { v ->
            dismiss()
            leftListener?.onClick(v)
        }
        mTvConfirm!!.setOnClickListener { v ->
            dismiss()
            rightListener?.onClick(v)
        }
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.setCancelable(true)
        dialog!!.setCanceledOnTouchOutside(true)
        dialog!!.show()
    }

    fun showInputDialog(
        title: String?,
        hintMsg: String?,
        showCancelBtn: Boolean,
        callback: InputCallback?
    ) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_input)
        initInputView(dialog!!)
        tv_title!!.text = title
        et_content!!.hint = hintMsg
        //        et_content.setMovementMethod(ScrollingMovementMethod.getInstance());
        if (!showCancelBtn) {
            mTvCancel!!.visibility = View.GONE
        } else {
            mTvCancel!!.visibility = View.VISIBLE
        }
        mTvCancel!!.setOnClickListener {
            if (et_content!!.hasFocus()) {
                hideSoftKeyboard(mContext, et_content)
            }
            dismiss()
        }
        mTvConfirm!!.setOnClickListener(View.OnClickListener {
            if (isEmpty(et_content!!.text.toString())) {
                Toast.makeText(mContext, hintMsg, Toast.LENGTH_SHORT).show()
                return@OnClickListener
            }
            if (et_content!!.hasFocus()) {
                hideSoftKeyboard(mContext, et_content)
            }
            callback?.onInputCallback(et_content!!.text.toString())
            dismiss()
        })
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.3)
        }
        dialog!!.setCancelable(false)
        dialog!!.setCanceledOnTouchOutside(false)
        dialog!!.show()
    }

    fun showInputDialogWithOld(title: String?, text: String, callback: InputCallback?) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_input)
        initInputView(dialog!!)
        tv_title!!.text = title
        et_content!!.setText(text)
        et_content!!.setSelection(text.length)
        et_content!!.movementMethod = ScrollingMovementMethod.getInstance()
        mTvCancel!!.setOnClickListener {
            if (et_content!!.hasFocus()) {
                hideSoftKeyboard(mContext, et_content)
            }
            dismiss()
        }
        mTvConfirm!!.setOnClickListener {
            if (et_content!!.hasFocus()) {
                hideSoftKeyboard(mContext, et_content)
            }
            callback?.onInputCallback(et_content!!.text.toString())
            dismiss()
        }
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.setCancelable(true)
        dialog!!.setCanceledOnTouchOutside(true)
        dialog!!.show()
    }

    private fun initView(dialog: Dialog) {
        mTvContent = dialog.findViewById<View>(R.id.tv_content) as TextView
        mTvCancel = dialog.findViewById<View>(R.id.tv_cancel) as TextView
        mSpiltLine = dialog.findViewById(R.id.spilt_line)
        mTvConfirm = dialog.findViewById<View>(R.id.tv_confirm) as TextView
    }

    private fun initImgDialogView(dialog: Dialog) {
        mTvContent = dialog.findViewById<View>(R.id.tv_content) as TextView
        mTvCancel = dialog.findViewById<View>(R.id.tv_cancel) as TextView
        mSpiltLine = dialog.findViewById(R.id.spilt_line)
        mTvConfirm = dialog.findViewById<View>(R.id.tv_confirm) as TextView
        iv_img = dialog.findViewById(R.id.iv_img)
    }

    private fun initUpdateDownloadView(dialog: Dialog) {
        pb_loading = dialog.findViewById<View>(R.id.pb_loading) as ProgressBar
        btn_cancel = dialog.findViewById<View>(R.id.btn_cancel) as TextView
        tv_progress = dialog.findViewById<View>(R.id.tv_progress) as TextView
    }

    private fun initInputView(dialog: Dialog) {
        tv_title = dialog.findViewById<View>(R.id.tv_title) as TextView
        et_content = dialog.findViewById<View>(R.id.et_content) as EditText
        mTvCancel = dialog.findViewById<View>(R.id.tv_cancel) as TextView
        mSpiltLine = dialog.findViewById(R.id.spilt_line)
        mTvConfirm = dialog.findViewById<View>(R.id.tv_confirm) as TextView
    }

    fun dismiss() {
        if (dialog != null && dialog!!.isShowing) {
            dialog!!.dismiss()
        }
    }

    private fun setDialogSize(context: Context) {
        val window = dialog!!.window
        window!!.setGravity(Gravity.CENTER)
        val m = (context as Activity).windowManager
        val d = m.defaultDisplay // 获取屏幕宽、高用
        val p = window.attributes // 获取对话框当前的参数值
        p.width = (d.width * 0.8).toInt() // 宽度设置为屏幕的weight
        window.setBackgroundDrawableResource(android.R.color.transparent)
        window.attributes = p
        dialog!!.setCanceledOnTouchOutside(false)
        dialog!!.setCancelable(false)
    }

    /**
     * 自定义布局dialog
     */
    fun setDialogSize(context: Context, weight: Double, height: Double) {
        val window = dialog!!.window
        window!!.setGravity(Gravity.CENTER)
        val m = (context as Activity).windowManager
        val d = m.defaultDisplay // 获取屏幕宽、高用
        val p = window.attributes // 获取对话框当前的参数值
        p.width = (d.width * weight).toInt() // 宽度设置为屏幕的weight
        p.height = (d.height * height).toInt() // 宽度设置为屏幕的weight
        window.setBackgroundDrawableResource(android.R.color.transparent)
        window.attributes = p
        dialog!!.setCanceledOnTouchOutside(false)
        dialog!!.setCancelable(false)
    }

    interface InputCallback {
        fun onInputCallback(str: String?)
    }

    /**
     * 错误弹框
     * @param msg
     * @param rightListener
     */
    fun showErrorDialog(msg: String?, rightListener: View.OnClickListener?) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        dialog!!.setContentView(R.layout.view_dialog_app_error)
        mTvContent = dialog!!.findViewById(R.id.tv_content)
        mTvConfirm = dialog!!.findViewById(R.id.tv_confirm)
        mTvContent?.setText(msg)
        mTvContent?.setMovementMethod(ScrollingMovementMethod.getInstance())
        mTvConfirm?.setOnClickListener(View.OnClickListener { v ->
            dismiss()
            rightListener?.onClick(v)
        })
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.setCancelable(true)
        dialog!!.setCanceledOnTouchOutside(true)
        dialog!!.show()
    }

    /**
     * 通用自定义提示框
     * @param msg
     * @param leftTxt
     * @param rightTxt
     * @param showCancelBtn
     * @param leftListener
     * @param rightListener
     */
    fun showCommonDialog(
        msg: String?,
        leftTxt: String?,
        rightTxt: String?,
        showTopImg: Boolean,
        showCancelBtn: Boolean,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        if (showTopImg) {
            dialog!!.setContentView(R.layout.view_dialog_app_error)
        } else {
            dialog!!.setContentView(R.layout.view_dialog_app_hint)
        }
        mTvContent = dialog!!.findViewById(R.id.tv_content)
        mTvConfirm = dialog!!.findViewById(R.id.tv_confirm)
        mTvCancel = dialog!!.findViewById(R.id.tv_cancel)
        mSpiltLine = dialog!!.findViewById(R.id.spilt_line)
        mTvCancel?.setVisibility(if (showCancelBtn) View.VISIBLE else View.GONE)
        mSpiltLine?.setVisibility(if (showCancelBtn) View.VISIBLE else View.GONE)
        mTvContent?.setText(msg)
        mTvContent?.setMovementMethod(ScrollingMovementMethod.getInstance())
        mTvCancel?.setText(leftTxt)
        mTvConfirm?.setText(rightTxt)
        mTvCancel?.setOnClickListener(View.OnClickListener { v ->
            dismiss()
            leftListener?.onClick(v)
        })
        mTvConfirm?.setOnClickListener(View.OnClickListener { v ->
            dismiss()
            rightListener?.onClick(v)
        })
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.setCancelable(true)
        dialog!!.setCanceledOnTouchOutside(true)
        dialog!!.show()
    }

    /**
     * 通用自定义提示框
     * @param msg
     * @param leftTxt
     * @param rightTxt
     * @param showCancelBtn
     * @param leftListener
     * @param rightListener
     */
    fun showCommonDialog(
        msg: String?,
        leftTxt: String?,
        rightTxt: String?,
        showTopImg: Boolean,
        showCancelBtn: Boolean,
        allowCancelOnTouchOutside: Boolean,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        if (showTopImg) {
            dialog!!.setContentView(R.layout.view_dialog_app_error)
        } else {
            dialog!!.setContentView(R.layout.view_dialog_app_hint)
        }
        mTvContent = dialog!!.findViewById(R.id.tv_content)
        mTvConfirm = dialog!!.findViewById(R.id.tv_confirm)
        mTvCancel = dialog!!.findViewById(R.id.tv_cancel)
        mSpiltLine = dialog!!.findViewById(R.id.spilt_line)
        mTvCancel?.setVisibility(if (showCancelBtn) View.VISIBLE else View.GONE)
        mSpiltLine?.setVisibility(if (showCancelBtn) View.VISIBLE else View.GONE)
        mTvContent?.setText(msg)
        mTvContent?.setMovementMethod(ScrollingMovementMethod.getInstance())
        mTvCancel?.setText(leftTxt)
        mTvConfirm?.setText(rightTxt)
        mTvCancel?.setOnClickListener(View.OnClickListener { v ->
            dismiss()
            leftListener?.onClick(v)
        })
        mTvConfirm?.setOnClickListener(View.OnClickListener { v ->
            dismiss()
            rightListener?.onClick(v)
        })
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.setCancelable(allowCancelOnTouchOutside)
        dialog!!.setCanceledOnTouchOutside(allowCancelOnTouchOutside)
        dialog!!.show()
    }

    /**
     * 通用自定义提示框
     * @param msg
     * @param leftTxt
     * @param rightTxt
     * @param showCancelBtn
     * @param leftListener
     * @param rightListener
     */
    fun showCommonDialog(
        title: String?,
        msg: String?,
        leftTxt: String?,
        rightTxt: String?,
        showTopImg: Boolean,
        showCancelBtn: Boolean,
        allowCancelOnTouchOutside: Boolean,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        dismiss()
        dialog = Dialog(mContext, R.style.prompt_dialog)
        if (showTopImg) {
            dialog!!.setContentView(R.layout.view_dialog_app_error)
        } else {
            dialog!!.setContentView(R.layout.view_dialog_app_hint)
        }
        mTvTopHint = dialog!!.findViewById(R.id.tv_topHint)
        mTvContent = dialog!!.findViewById(R.id.tv_content)
        mTvConfirm = dialog!!.findViewById(R.id.tv_confirm)
        mTvCancel = dialog!!.findViewById(R.id.tv_cancel)
        mSpiltLine = dialog!!.findViewById(R.id.spilt_line)
        mTvCancel?.setVisibility(if (showCancelBtn) View.VISIBLE else View.GONE)
        mSpiltLine?.setVisibility(if (showCancelBtn) View.VISIBLE else View.GONE)
        mTvTopHint?.setText(title)
        mTvContent?.setText(msg)
        mTvContent?.setMovementMethod(ScrollingMovementMethod.getInstance())
        mTvCancel?.setText(leftTxt)
        mTvConfirm?.setText(rightTxt)
        mTvCancel?.setOnClickListener(View.OnClickListener { v ->
            dismiss()
            leftListener?.onClick(v)
        })
        mTvConfirm?.setOnClickListener(View.OnClickListener { v ->
            dismiss()
            rightListener?.onClick(v)
        })
        if (isLandscape(mContext)) {
            setDialogSize(mContext, 0.6, 0.6)
        } else {
            setDialogSize(mContext, 0.8, 0.5)
        }
        dialog!!.setCancelable(allowCancelOnTouchOutside)
        dialog!!.setCanceledOnTouchOutside(allowCancelOnTouchOutside)
        dialog!!.show()
    }
}